package com.vtc.session4;

import com.vtc.R;
import com.vtc.session3.Employee;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListView2Actvity extends Activity implements OnItemClickListener,TabListener {

	private ListView lv;
	public static ListView2Adapter adapter;
	@SuppressLint({ "InlinedApi", "NewApi" })
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.listview_activity);
		
		lv =(ListView) findViewById(R.id.lvEmployee);
		if(adapter==null) adapter= new ListView2Adapter(this);
		lv.setAdapter(adapter);		
		// dang ky su kien
		lv.setOnItemClickListener(this);
		System.out.println("Khoi tao");
		
		
//		ActionBar actionBar = getActionBar();
//	    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//	    
//	    actionBar.addTab(actionBar.newTab().setText("ABC").setTabListener(this));
//	    actionBar.addTab(actionBar.newTab().setText("XYZ").setTabListener(this));
//	    actionBar.addTab(actionBar.newTab().setText("ABC").setTabListener(this));
	    
//	    actionBar.setDisplayHomeAsUpEnabled(false);
//	    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
	    

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int postion, long id) {
		ListView2Adapter adapter = (ListView2Adapter)adapterView.getAdapter();
		System.out.println("Ok, da vao");
		Employee e = (Employee) adapter.getItem(postion);
		Intent i = new Intent(this,AddActivity.class);				
		i.putExtra("xxx",e);
		
		startActivityForResult(i, 123);
	}
//	@SuppressLint("NewApi")
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater menuInflater = getMenuInflater();
		
		menuInflater.inflate(R.menu.listview2_menu, menu);
	    
		return true;
	}
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		
		switch(item.getItemId()){
		    case R.id.itemAdd:
		    	Intent i = new Intent(this,AddActivity.class);
		    	startActivity(i);
			    break;
			    
		    case R.id.itemDel:
		    	this.adapter.delete();
		    	Toast.makeText(this, "Delele completed!", Toast.LENGTH_LONG).show();
		    	break;
		}
		return false;
	}

	@SuppressLint("NewApi")
	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		
		// TODO Auto-generated method stub
		if(tab.getText().equals("ABC")){
			getFragmentManager().beginTransaction().replace(R.id.lvEmployee, new MyFrament1()).commit();
		}
		else if(tab.getText().equals("XYZ")){
			getFragmentManager().beginTransaction().replace(R.id.lvEmployee, new MyFrament2()).commit();
		}
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}
}
